package com.darker.yygh.controller.api;

import com.atguigu.yygh.model.hosp.Department;
import com.atguigu.yygh.model.hosp.Hospital;
import com.atguigu.yygh.model.hosp.Schedule;
import com.atguigu.yygh.vo.hosp.DepartmentQueryVo;
import com.atguigu.yygh.vo.hosp.ScheduleQueryVo;
import com.darker.yygh.common.helper.HttpRequestHelper;
import com.darker.yygh.common.util.MD5;
import com.darker.yygh.exception.YyghException;
import com.darker.yygh.result.Result;
import com.darker.yygh.result.ResultCodeEnum;
import com.darker.yygh.service.DepartmentService;
import com.darker.yygh.service.HospitalService;
import com.darker.yygh.service.HospitalSetService;
import com.darker.yygh.service.ScheduleService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Api("医院管理API接口")
@RestController
@RequestMapping("/api/hosp")
public class ApiController {
    @Autowired
    HospitalService hospitalService;
    @Autowired
    HospitalSetService hospitalSetService;

    @Autowired
    DepartmentService departmentService;

    @Autowired
    ScheduleService scheduleService;

    //查询医院
    @PostMapping("hospital/show")
    public Result getHospital(HttpServletRequest request) {
        //获取传递过来的医院信息
        Map<String, String[]> parameterMap = request.getParameterMap();
        //转换object方便处理
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);
        String sign = (String) map.get("sign");
        //把查询的值做md5加密再对比
        String hoscode = (String) map.get("hoscode");
        String signKey = hospitalSetService.getSignKey(hoscode);
        String signKeyMd5 = MD5.encrypt(signKey);
        //比对密钥再执行方法
        if (!sign.equals(signKeyMd5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        Hospital hospital = hospitalService.getByHoscode(hoscode);
        return Result.ok(hospital);
    }

    //上传医院接口
    @PostMapping("saveHospital")
    public Result saveHosp(HttpServletRequest request) {
        //获取传递过来的医院信息
        Map<String, String[]> parameterMap = request.getParameterMap();
        //转换object方便处理
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);
        String sign = (String) map.get("sign");
        //把查询的值做md5加密再对比
        String hoscode = (String) map.get("hoscode");
        String signKey = hospitalSetService.getSignKey(hoscode);
        String signKeyMd5 = MD5.encrypt(signKey);
        //比对密钥再执行方法
        if (!sign.equals(signKeyMd5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        //传输过程中图片做了base64编码 使“+”转换为“ ” 因此我们要转换回来
        String logoData = (String) map.get("logoData");
        //把空格” “替换成加号”+“
        logoData = logoData.replace(" ", "+");
        map.put("logoData", logoData);
        hospitalService.save(map);
        return Result.ok();
    }

    //上传科室接口
    @PostMapping("saveDepartment")
    public Result saveDepartment(HttpServletRequest request) {
        //获取传递过来的医院信息
        Map<String, String[]> parameterMap = request.getParameterMap();
        //转换object方便处理
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);
        String sign = (String) map.get("sign");
        //把查询的值做md5加密再对比
        String hoscode = (String) map.get("hoscode");
        String signKey = hospitalSetService.getSignKey(hoscode);
        String signKeyMd5 = MD5.encrypt(signKey);
        //比对密钥再执行方法
        if (!sign.equals(signKeyMd5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        departmentService.save(map);
        return Result.ok();
    }

    //分页查询科室
    @PostMapping("/department/list")
    public Result findDepartment(HttpServletRequest request) {
        Map<String, String[]> parameterMap = request.getParameterMap();
        //转换object方便处理
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);
        String sign = (String) map.get("sign");
        //把查询的值做md5加密再对比
        String hoscode = (String) map.get("hoscode");
        String signKey = hospitalSetService.getSignKey(hoscode);
        String signKeyMd5 = MD5.encrypt(signKey);
        //比对密钥再执行方法
        if (!sign.equals(signKeyMd5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        Integer page = Integer.valueOf((String) map.get("page"));
        Integer limit = Integer.valueOf((String) map.get("limit"));
        if (page == null) {
            page = 1;
        }
        if (limit == null) {
            limit = 1;
        }
        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hoscode);
        Page<Department> pageDepartment = departmentService.findPageDepartment(page, limit, departmentQueryVo);
        return Result.ok(pageDepartment);
    }

    //删除科室
    @PostMapping("/department/remove")
    public Result removeDepartment(HttpServletRequest request) {
        Map<String, String[]> parameterMap = request.getParameterMap();
        //转换object方便处理
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);
        String sign = (String) map.get("sign");
        //把查询的值做md5加密再对比
        String hoscode = (String) map.get("hoscode");
        String signKey = hospitalSetService.getSignKey(hoscode);
        String signKeyMd5 = MD5.encrypt(signKey);
        //比对密钥再执行方法
        if (!sign.equals(signKeyMd5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        String depcode = (String) map.get("depcode");//科室编号
        departmentService.remove(hoscode, depcode);
        return Result.ok();
    }

    //上传排班接口
    @PostMapping("saveSchedule")
    public Result saveSchedule(HttpServletRequest request) {
        Map<String, String[]> parameterMap = request.getParameterMap();
        //转换object方便处理
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);
        String sign = (String) map.get("sign");
        //把查询的值做md5加密再对比
        String hoscode = (String) map.get("hoscode");
        String signKey = hospitalSetService.getSignKey(hoscode);
        String signKeyMd5 = MD5.encrypt(signKey);
        //比对密钥再执行方法
        if (!sign.equals(signKeyMd5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        scheduleService.save(map);
        return Result.ok();
    }
    //分页查询排班
    @PostMapping("schedule/list")
    public Result findSchedule(HttpServletRequest request){
        Map<String, String[]> parameterMap = request.getParameterMap();
        //转换object方便处理
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);
        String sign = (String) map.get("sign");
        //把查询的值做md5加密再对比
        String hoscode = (String) map.get("hoscode");
        String signKey = hospitalSetService.getSignKey(hoscode);
        String signKeyMd5 = MD5.encrypt(signKey);
        //比对密钥再执行方法
        if (!sign.equals(signKeyMd5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        Integer page = Integer.valueOf( (String)map.get("page"));
        Integer litmit = Integer.valueOf( (String)map.get("limit"));
        if(page==null){
            page=1;
        }
        if(litmit==null){
            litmit=10;
        }
        ScheduleQueryVo scheduleQueryVo=new ScheduleQueryVo();
        scheduleQueryVo.setHoscode(hoscode);
        Page<Schedule> schedulePage=scheduleService.findSchedule(page,litmit,scheduleQueryVo);
        return Result.ok(schedulePage);
    }
    //删除科室排班
    @PostMapping("schedule/remove")
    public Result removeSchedule(HttpServletRequest request){
        Map<String, String[]> parameterMap = request.getParameterMap();
        //转换object方便处理
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);
        String sign = (String) map.get("sign");
        //把查询的值做md5加密再对比
        String hoscode = (String) map.get("hoscode");
        String signKey = hospitalSetService.getSignKey(hoscode);
        String signKeyMd5 = MD5.encrypt(signKey);
        //比对密钥再执行方法
        if (!sign.equals(signKeyMd5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        String hosScheduleId = (String) map.get("hosScheduleId");
        scheduleService.removeSchedule(hoscode,hosScheduleId);
       return Result.ok();
    }
}
